import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '_views/Home'
import { isLogin } from '_utils/token.js'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    component: Home,
    meta: {
      needLogin: false,
      keepAlived: true // 缓存
    }
  },
  {
    path: '/cart',
    component: () => import('_views/Cart'),
    meta: {
      needLogin: true
    }
  },
  {
    path: '/cate',
    component: () => import('_views/Cate'),
    meta: {
      needLogin: false
    }
  },
  {
    path: '/rank',
    component: () => import('_views/Rank'),
    meta: {
      needLogin: false
    }
  },
  {
    path: '/user',
    component: () => import('_views/User'),
    meta: {
      needLogin: true
    }
  },
  {
    path: '/detail/:id',
    component: () => import('_views/Detail'),
    meta: {
      needLogin: false
    }
  },
  {
    path: '/list',
    component: () => import('_views/ItemList'),
    meta: {
      needLogin: false
    }
  },
  {
    path: '/login',
    component: () => import('_views/Login'),
    meta: {
      needLogin: false
    }
  },
  {
    path: '/register',
    component: () => import('_views/Register'),
    meta: {
      needLogin: false
    }
  },
  {
    path: '/search',
    component: () => import('_views/Search'),
    meta: {
      needLogin: false
    }
  },
  {
    path: '/searchlist',
    component: () => import('_views/SearchList'),
    meta: {
      needLogin: false
    }
  },
  // 添加地址
  {
    path: '/addaddress',
    name: '新增收货地址',
    component: () => import('_views/AddressAdd'),
    meta: {
      needLogin: true
    }
  },
  // 收货地址列表
  {
    path: '/addresssMange',
    name: '收货地址管理',
    component: () => import('_views/AddressList'),
    meta: {
      needLogin: true
    }
  },
  {
    path: '/addEdit',
    name: '收货地址编辑',
    component: () => import('_views/AddressEdit'),
    meta: {
      needLogin: true
    }
  },
  {
    path: '/set',
    name: '账户设置',
    component: () => import('_views/Set'),
    meta: {
      needLogin: true
    }
  },
  {
    path: '/permes',
    name: '个人信息',
    component: () => import('_views/PersonMessage'),
    meta: {
      needLogin: true
    }
  },
  {
    path: '/nickname',
    name: '昵称',
    component: () => import('_views/NickName'),
    meta: {
      needLogin: true
    }
  },
  {
    path: '/order',
    name: '确认订单',
    component: () => import('_views/Order'),
    meta: {
      needLogin: true
    }
  },
  {
    path: '/orderAdd',
    name: '添加联系人',
    component: () => import('_views/OrderAddressAdd'),
    meta: {
      needLogin: true
    }
  },
  {
    path: '/orderAddList',
    name: '添加联系人',
    component: () => import('_views/OrderAddressList'),
    meta: {
      needLogin: true
    }
  },
  {
    path: '/orderAddEdit',
    name: '添加联系人',
    component: () => import('_views/OrderAddressEdit'),
    meta: {
      needLogin: true
    }
  }

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  if (to.meta.needLogin) {
    // 需要登录
    if (isLogin()) {
      // 登录了
      next()
    } else {
      // 没有登录
      next({
        path: '/login',
        query: {
          from: to.path
        }
      })
    }
  } else {
    // 不需要登录
    next()
  }
})

export default router
